package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.*;
import com.woniuxy.carrental.service.PurchaseService;
import com.woniuxy.carrental.service.SupplyCarService;
import com.woniuxy.carrental.service.SupplyPartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xilei
 * @create 2021-10-05 上午 8:39
 */
@RestController
public class PurchaseController {
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    SupplyCarService supplyCarService;
    @Autowired
    SupplyPartService supplyPartService;


    @Transactional
    @PostMapping("/purchaselist/{supply_id}")
    public ResponseResult addPurchase(@RequestBody Purchase purchase,@PathVariable("supply_id")Integer supply_id){
        System.out.println("要加入数据库的车辆采购信息为："+purchase);
        purchaseService.insertPurchase(CommonUtil.uuid(), new Date(), purchase.getCar().getCar_name(), null,purchase.getSupplier().getSupplier_name(), purchase.getCar_count(), 0, 0, purchase.getCar_allsprice(),true);
        Supply_car supply_car = supplyCarService.findSupply_carBysupply_id(supply_id);
        supplyCarService.updateSupply_carBysupply_id(supply_id, supply_car.getAllcount()-purchase.getCar_count());
        return new ResponseResult(null, 200, "OK");
    }

    @Transactional
    @PostMapping("/purchaselist1/{supply_part_id}")
    public ResponseResult addPurchase1(@RequestBody Purchase purchase,@PathVariable("supply_part_id") Integer supply_part_id){
        System.out.println("要加入数据库的汽配采购信息为："+purchase);
        purchaseService.insertPurchase(CommonUtil.uuid(), new Date(), null, purchase.getCarpart().getCarpart_name(), purchase.getSupplier().getSupplier_name(), 0, purchase.getPart_count(), purchase.getPart_allsprice(), 0,true);
        Supply_part supply_part = supplyPartService.findSupply_partBysupply_part_id(supply_part_id);
        supplyPartService.updateSupply_partBysupply_part_id(supply_part_id, supply_part.getPart_allcount()-purchase.getPart_count());
        return new ResponseResult(null, 200, "OK");
    }

    @DeleteMapping("/purchaselist")
    public ResponseResult deletePurchase(String purchase_id){
        System.out.println("要删除的采购记录编号为："+purchase_id);
        purchaseService.deletePurchase(purchase_id);
        return new ResponseResult(null, 200, "删除成功");
    }

    @GetMapping("/purchaselistgetall")
    public ResponseResult<PageBean<Purchase>> findALlPurchase(Integer pageIndex,Integer pageSize){
        List<Purchase> list = purchaseService.findPurchaseByPage(pageIndex, pageSize);
        for(Purchase a:list){
            System.out.println(a);
        }
        PageBean<Purchase> pageBean = new PageBean<>();
        pageBean.setBeanList(list);
        pageBean.setPageSize(pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setTotalRecord(purchaseService.findPurchase().size());
        return new ResponseResult<>(pageBean, 200, "OK");
    }

    @GetMapping("/purchaselistgetallcar")
    public ResponseResult<PageBean<Purchase>> findALlPurchaseByParam(Integer purchase_id, Date purchase_date,
                                                                     String car_name, String carpart_name,
                                                                     String supplier_name, Integer pageIndex,
                                                                     Integer pageSize){
        List<Purchase> list = purchaseService.findPurchaseByPageByParam(purchase_id, purchase_date,
                car_name, carpart_name, supplier_name, pageIndex, pageSize);
        for(Purchase a:list){
            System.out.println(a);
        }
        PageBean<Purchase> pageBean = new PageBean<>();
        pageBean.setBeanList(list);
        pageBean.setPageSize(pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setTotalRecord(purchaseService.findPurchase().size());
        return new ResponseResult<>(pageBean, 200, "OK");
    }

    @GetMapping("/purchaselist")
    public ResponseResult findAllChart(){
        List<Purchase> purchase = purchaseService.findPurchase();
        Map<Date, Integer> map1 = getIdNameMap(purchase);
        System.out.println(map1);
        List<AAA> collect = new ArrayList<>();
        for (Map.Entry<Date, Integer> e : map1.entrySet()) {
            AAA aaa = new AAA(e.getKey(), e.getValue());
            collect.add(aaa);
        }
        System.out.println(collect);
        return new ResponseResult<>(collect, 200, "OK");
    }

    public Map<Date, Integer> getIdNameMap(List<Purchase> purchase){

        return purchase.stream().collect(Collectors.toMap(Purchase::getPurchase_date, Purchase::getPart_allsprice,(entity1, entity2) -> entity1+entity2,TreeMap::new));
    }

    @GetMapping("/purchaselistall")
    public ResponseResult findAllChartall(){
        List<CCC> by111 = purchaseService.findPurchaseBy111();
        for(CCC a:by111){
            a.setCarall(a.getCar_allsprice()+a.getPart_allsprice());
            System.out.println(a);
        }
        Map<Date, Integer> map1 = getIdNameMap1(by111);
        List<AAA> collect = new ArrayList<>();
        for (Map.Entry<Date, Integer> e : map1.entrySet()) {
            AAA aaa = new AAA(e.getKey(), e.getValue());
            collect.add(aaa);
        }
        System.out.println(collect);
        return new ResponseResult<>(collect, 200, "OK");
    }

    public Map<Date, Integer> getIdNameMap1(List<CCC> ccc){
        return ccc.stream().collect(Collectors.toMap(CCC::getPurchase_date, CCC::getCarall,(entity1, entity2) -> entity1+entity2,TreeMap::new));
    }
}
